var express = require('express'),
  app = express(),
  server = require('http').createServer(app),
  io = require('socket.io')(server);

// 开启服务器
app.listen(8000, () => {
  console.log('socketServer start')
})

app.use('/', express.static(__dirname + '/server'))


// 配置跨域
app.use((req, res, next) => {
  // 允许的域
  res.header("Access-Control-Allow-Origin", "http://192.168.0.113:5500"); // http://192.168.0.113:5500
  // res.header("Access-Control-Allow-Origin", "*"); // 允许任何域
  // 允许凭证
  res.header("Access-Control-Allow-Credentials", true);
  // 允许表头
  res.header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, Accept, X-Requested-With");
  // 
  res.header("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, HEAD, OPTIONS");
  if (req.method === 'OPTIONS') {
    res.send('OK!');
    return;
  }
  next();
})



io.on('connection', function (socket) {
  socket.on('foo', function (data) {
    console.log(data)
  })
})